package javase.anamorphic_words;

/**
 * *1. 给定两个字符串 s 和 t ，编写一个函数来判断它们是不是一组变位词（字母异位词）。**
 * *注意：若 s 和 t 中每个字符出现的次数都相同且字符顺序不完全相同，则称 s 和 t 互为变位词（字母异位词）
 * 示例 1:
 * 	输入: s = "anagram", t = "nagaram"
 * 	输出: true
 * 示例 2:
 * 	输入: s = "rat", t = "car"
 * 	输出: false
 * 示例 3:
 * 	输入: s = "a", t = "a"
 * 	输出: false
 */
class AnamorphicWords {
    public static void main(String[] args) {
        String s = "anagram";
        String t = "nagaram";
        System.out.println(numberOfCharacters(s, t));
    }

    /**
     * 判断两个字符数量是否相同
     */
    public static boolean numberOfCharacters(String s1,String s2){
        boolean flag = false;
        //1.如果字符串长度不相等
        if (s1.length() != s2.length()) return false;
        //2.如果长度为1
        if (s1.length() == 1 || s2.length() == 1) return false;
        //3.判断字符串是否乱序
        for (int i = 0; i < s1.length(); i++) {
            if (s1.charAt(i) != s2.charAt(i)){
                flag = true;
                break;
            }
        }
        //1)乱序 2)长度不为1
        if (flag) {
            //对字符串进行比较
            char[] char1 = CharSort(s1);
            char[] char2 = CharSort(s2);
            for (int i = 0; i < char1.length; i++) {
                if (char1[i] != char2[i])
                    return false;
            }
            return true;
        }
        return false;
    }

    public static char[] CharSort(String str){
        // 字符串转换成字符数组
        char[] chars = str.toCharArray();
        // 冒泡排序
        for (int i = 0; i < chars.length-1; i++) {
            for (int j = 0; j < chars.length-1-i; j++) {
                if(chars[j] > chars[j+1]){
                    char temp = chars[j];
                    chars[j] = chars[j+1];
                    chars[j+1] = temp;
                }
            }
        }
        return chars;
    }
}
